package models

import (
	"go.mongodb.org/mongo-driver/bson"
	"hospital-srv/basic/inits"
	"time"
)

type HealthDataSummarize struct {
	Id                    uint64    `bson:"id" gorm:"column:id;type:bigint UNSIGNED;comment:主键id;primaryKey;not null;" json:"id"`                                                    // 主键id
	UserId                uint64    `bson:"userId" gorm:"column:user_id;type:bigint UNSIGNED;comment:用户Id;not null;" json:"user_id"`                                                 // 用户Id
	EquipmentId           uint64    `bson:"equipmentId" gorm:"column:equipment_id;type:bigint UNSIGNED;comment:设备Id;not null;" json:"equipment_id"`                                  // 设备Id
	BodyTemperature       float32   `bson:"bodyTemperature" gorm:"column:body_temperature;type:float(10, 2);comment:体温  /℃  36.1℃-37.2℃;default:NULL;" json:"body_temperature"`      // 体温  /℃  36.1℃-37.2℃
	BloodSugar            float32   `bson:"bloodSugar" gorm:"column:blood_sugar;type:float(10, 2);comment:血糖   空腹3.9-6.1mmol/L 正常小于11.1mmol;default:NULL;" json:"blood_sugar"` // 血糖   空腹3.9-6.1mmol/L 正常小于11.1mmol
	BloodPressure         float32   `bson:"bloodPressure" gorm:"column:blood_pressure;type:float(10, 2);comment:血压 收缩压90~140mmHg  ;default:NULL;" json:"blood_pressure"`          // 血压 收缩压90~140mmHg
	BloodPressureD        float32   `bson:"bloodPressureD" gorm:"column:blood_pressure_d;type:float(10, 2);comment:血压 舒张压60~90mmHg;default:NULL;" json:"blood_pressure_d"`        // 血压 舒张压60~90mmHg
	BloodOxygen           float32   `bson:"bloodOxygen" gorm:"column:blood_oxygen;type:float(10, 2);comment:血氧 ;default:NULL;" json:"blood_oxygen"`                                  // 血氧
	Date                  string    `bson:"date" gorm:"column:date;type:varchar(30);comment:总结的数据的日期;not null;" json:"date"`                                                   // 总结日期
	DateType              int8      `bson:"dateType" gorm:"column:date_type;type:tinyint(1);comment:日期分类  1-日总结 2-周总结 3-月总结;not null;" json:"date_type"`                  // 日期分类  1-日总结 2-周总结 3-月总结
	BodyTemperatureAdvice string    `bson:"bodyTemperatureAdvice" gorm:"column:date_type;type:varchar(100);comment:体温建议;" json:"body_temperature_advice"`
	BloodSugarAdvice      string    `bson:"bloodSugarAdvice" gorm:"column:date_type;type:varchar(100);comment:血糖建议;" json:"blood_sugar_advice"`
	BloodPressureAdvice   string    `bson:"bloodPressureAdvice" gorm:"column:date_type;type:varchar(100);comment:血压建议;" json:"blood_pressure_advice"`
	BloodOxygenAdvice     string    `bson:"bloodOxygenAdvice" gorm:"column:date_type;type:varchar(100);comment:血氧建议;" json:"blood_oxygen_advice"`
	CreatedAt             string `bson:"createdAt" gorm:"column:created_at;type:datetime(3);comment:创建时间;not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 创建时间
	UpdatedAt             string `bson:"updatedAt" gorm:"column:updated_at;type:datetime(3);comment:修改时间;not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"` // 修改时间
	DeletedAt             string `bson:"deletedAt" gorm:"column:deleted_at;type:datetime(3);comment:删除时间;default:NULL;" json:"deleted_at"`                          // 删除时间
}

func (h *HealthDataSummarize) TableName() string {
	return "health_data_summarize"
}

// mongodb 总结数据
func (h *HealthDataSummarize) MongoDbCreateSummarize() error {
	_, err := inits.MongoSum.InsertOne(inits.Ctx, &h)
	if err != nil {
		return err
	}
	return nil
}

// mongodb获取 某天时总结
func (h *HealthDataSummarize) MongoDbGetOneData() ([]HealthDataSummarize, error) {

	find, err := inits.MongoSum.Find(inits.Ctx, bson.M{"userId": h.UserId,
		"equipmentId": h.EquipmentId, "date": h.Date, "dateType": 1})
	if err != nil {
		return nil, err
	}

	var data []HealthDataSummarize
	err = find.All(inits.Ctx, &data)
	if err != nil {
		return nil, err
	}
	return data, err
}

// mongodb 获取七日每日总结数据
func (h *HealthDataSummarize) MongoDbGetLastWeekData() ([]HealthDataSummarize, error) {
	date := time.Now().AddDate(0, 0, -7).Format(time.DateOnly)

	find, err := inits.MongoSum.Find(inits.Ctx, bson.M{"userId": h.UserId,
		"equipmentId": h.EquipmentId, "date": bson.M{"$gte": date}, "dateType": 2})
	if err != nil {
		return nil, err
	}

	var data []HealthDataSummarize
	err = find.All(inits.Ctx, &data)
	if err != nil {
		return nil, err
	}
	return data, err
}

// mongodb 获取7周总结数据
func (h *HealthDataSummarize) MongoDbGetSevenWeekData() ([]HealthDataSummarize, error) {
	date := time.Now().AddDate(0, 0, -28).Format(time.DateOnly)
	find, err := inits.MongoSum.Find(inits.Ctx, bson.M{"userId": h.UserId,
		"equipmentId": h.EquipmentId, "dateType": 3, "date": bson.M{"$gte": date}})
	if err != nil {
		return nil, err
	}

	var data []HealthDataSummarize
	err = find.All(inits.Ctx, &data)
	if err != nil {
		return nil, err
	}
	return data, err
}

// 生成总结
func (h *HealthDataSummarize) CreateSummarize() error {
	return inits.DB.Create(&h).Error
}

// 七天的日总结
func (h *HealthDataSummarize) GetSevenSummarize() (data []HealthDataSummarize, err error) {
	err = inits.DB.Where("user_id = ? and equipment_id = ?", h.UserId, h.EquipmentId).
		Where("date_type = 1").Order("created_at desc").
		Limit(7).Find(&data).Error
	return
}

// 日总结列表
func (h *HealthDataSummarize) GetSummarizeList() (data []HealthDataSummarize, err error) {
	err = inits.DB.Where("user_id = ? and equipment_id = ?", h.UserId, h.EquipmentId).
		Where("date_type = 1").Order("created_at desc").
		Find(&data).Error
	return
}

// 周总结列表
func (h *HealthDataSummarize) GetSummarizeWeekList() (data []HealthDataSummarize, err error) {
	err = inits.DB.Where("user_id = ? and equipment_id = ?", h.UserId, h.EquipmentId).
		Where("date_type = 2").Order("created_at desc").
		Find(&data).Error
	return
}
